//
//  SureOrderSectionFooterView.m
//  5-22测试专门
//
//  Created by RaoBo on 2018/6/11.
//  Copyright © 2018年 关键词. All rights reserved.
//  h:200

#import "SureOrderSectionFooterView.h"
#import <Masonry.h>

@interface SureOrderSectionFooterView()
@property(nonatomic, strong) UIView *bgView;

// 1.小计
@property(nonatomic, strong) UIView *div1; // 共计2件商品 $799.0
@property(nonatomic, strong) UILabel *totalLb;
@property(nonatomic, strong) UILabel *totalPriceLb;
@property(nonatomic, strong) UIView *line1;


// 2.配送方式
@property(nonatomic, strong) UIView *div2; // 配送方式 自提 快递
@property(nonatomic, strong) UILabel *peiSongFangshiLb;
@property(nonatomic, strong) UIButton *kuaiDiButton; //快递
@property(nonatomic, strong) UIButton *ziTiButton; // 自提
@property(nonatomic, strong) UIView *line2;

//2-2 自提:提货地点
@property(nonatomic, strong) UIView *div5;
@property(nonatomic, strong) UILabel *tiHuoLb; // 提货地点。
@property(nonatomic, strong) UILabel *tiHuoAddressLB;
@property(nonatomic, strong) UIView *line5;


//3.运费
@property(nonatomic, strong) UIView *div3; // 运费  ￥6.0
@property(nonatomic, strong) UILabel *freightLb; // 运费:费八
@property(nonatomic, strong) UILabel *freightPriceLb; //
@property(nonatomic, strong) UIView *line3;

//买家留言
@property(nonatomic, strong) UIView *div4; // 买家留言 [就是好就是好就是好就是好]
@property(nonatomic, strong) UILabel *liuYanLb;// 留言
//@property(nonatomic, strong) UITextField *liuyanTextField; // 请输入留言内容
@property(nonatomic, strong) UITextField *testTF;

@property(nonatomic, strong) UIView *line4;
@end

@implementation SureOrderSectionFooterView

#pragma mark -  一初始化
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self.contentView addSubview:self.bgView];
    self.bgView.backgroundColor = [UIColor whiteColor];
    
    [self.bgView addSubview:self.div1];
    [self.div1 addSubview:self.totalLb];
    [self.div1 addSubview:self.totalLb];
    [self.div1 addSubview:self.totalPriceLb];
    [self.div1 addSubview:self.line1];
    //---------------------------------------------------
    
    [self.bgView addSubview:self.div2];
    [self.div2 addSubview:self.peiSongFangshiLb];
    [self.div2 addSubview:self.kuaiDiButton];
    [self.div2 addSubview:self.ziTiButton];
    [self.div2 addSubview:self.line2];
    //---------------------------------------------------
    
    [self.bgView addSubview:self.div5]; // 提货地点,选择自提的时候显示
    [self.div5 addSubview:self.tiHuoLb];
    [self.div5 addSubview:self.tiHuoAddressLB];
    [self.div5 addSubview:self.line5];
    
    
    //---------------------------------------------------
    
    [self.bgView addSubview:self.div3];
    [self.div3 addSubview:self.freightLb];
    [self.div3 addSubview:self.freightPriceLb];
    [self.div3 addSubview:self.line3];
    //---------------------------------------------------
    
    [self.bgView addSubview:self.div4];
    self.div4.backgroundColor = [UIColor redColor];
    self.div4.userInteractionEnabled = YES;
    [self.div4 addSubview:self.liuYanLb];
//    [self.div4 addSubview:self.liuyanTextField];
    [self.div4 addSubview:self.line4];
    [self.div4 addSubview:self.testTF];
    //---------------------------------------------------
}



// 17个控件 chun
#pragma mark -  二lazy
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
    }
    return _bgView;
}
- (UIView *)div1{
    if (!_div1) {
        _div1 = [UIView new];
    }
    return _div1;
}
- (UILabel *)totalLb {
    if (!_totalLb) {
        _totalLb = [UILabel new];
        _totalLb.text = @"共计1件商品, 小计";
    }
    return _totalLb;
}
- (UILabel *)totalPriceLb {
    if (!_totalPriceLb) {
        _totalPriceLb = [UILabel new];
        _totalPriceLb.text = @"￥666.00";
        _totalPriceLb.font = [UIFont systemFontOfSize:20.0];
        _totalPriceLb.textColor = [UIColor redColor];
    }
     return _totalPriceLb;
}
- (UIView *)line1 {
    if (!_line1) {
        _line1 = [UIView new];
        _line1.backgroundColor = XJCOLOR_RGB(240, 240, 240);
    }
    return _line1;
}
//---------------------------------------------------



- (UIView *)div2 {
    if (!_div2) {
        _div2 = [UIView new];
    }
    return _div2;
}
- (UILabel *)peiSongFangshiLb {
    if (!_peiSongFangshiLb) {
        _peiSongFangshiLb = [UILabel new];
        _peiSongFangshiLb.text = @"配送方式";
    }
    return _peiSongFangshiLb;
}

// w:50  h:40
- (UIButton *)kuaiDiButton {
    if (!_kuaiDiButton) {
        _kuaiDiButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_kuaiDiButton setTitle:@"快递" forState:(UIControlStateNormal)];
        [_kuaiDiButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        
        [_kuaiDiButton setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:(UIControlStateNormal)];
        [_kuaiDiButton setBackgroundImage:[self imageWithColor:XJCOLOR_RGB(255, 218, 67)] forState:(UIControlStateSelected)];
        _kuaiDiButton.layer.borderColor = [UIColor blackColor].CGColor;
        _kuaiDiButton.layer.borderWidth = 1.0;
        _kuaiDiButton.layer.cornerRadius = 15;
        _kuaiDiButton.layer.masksToBounds = YES;
        
        [_kuaiDiButton addTarget:self action:@selector(kuaiDiButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
    return _kuaiDiButton;
}
- (UIButton *)ziTiButton {
    if (!_ziTiButton) {
        _ziTiButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _ziTiButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_ziTiButton setTitle:@"自提" forState:(UIControlStateNormal)];
        [_ziTiButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        
        [_ziTiButton setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:(UIControlStateNormal)];
        [_ziTiButton setBackgroundImage:[self imageWithColor:XJCOLOR_RGB(255, 218, 67)] forState:(UIControlStateSelected)];
        _ziTiButton.layer.borderColor = [UIColor blackColor].CGColor;
        _ziTiButton.layer.borderWidth = 1.0;
        
        _ziTiButton.layer.cornerRadius = 15;
        _ziTiButton.layer.masksToBounds = YES;
        
        [_ziTiButton addTarget:self action:@selector(ziTiButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _ziTiButton;
}
- (UIView *)line2 {
    if (!_line2) {
        _line2 = [UIView new];
        _line2.backgroundColor = XJCOLOR_RGB(240, 240, 240);
    }
    return _line2;
}
//---------------------------------------------------

//2-2 自提:提货地点
- (UIView *)div5 {
    if (!_div5) {
        _div5 = [UIView new];
    }
    return _div5;
}
- (UILabel *)tiHuoLb {
    if (!_tiHuoLb) {
        _tiHuoLb = [UILabel new];
        _tiHuoLb.text = @"提货地点";
    }
    return _tiHuoLb;
}

- (UILabel *)tiHuoAddressLB {
    if (!_tiHuoAddressLB) {
        _tiHuoAddressLB = [UILabel new];
        _tiHuoAddressLB.text = @"提货地点防静电可减肥的咖啡店华恒大华盛达就是好就是好就是好就是好就是好";
        _tiHuoAddressLB.font = [UIFont systemFontOfSize:14.0];
        _tiHuoAddressLB.numberOfLines = 0;
    }
    return _tiHuoAddressLB;
}
- (UIView *)line5{
    if (!_line5) {
        _line5 = [UIView new];
        _line5.backgroundColor = XJCOLOR_RGB(240, 240, 240);
    }
    return _line5;
}
//---------------------------------------------------









- (UIView *)div3 {
    if (!_div3) {
        _div3 = [UIView new];
    }
    return _div3;
}
- (UILabel *)freightLb {
    if (!_freightLb) {
        _freightLb = [UILabel new];
        _freightLb.text = @"运费";
    }
    return _freightLb;
}
- (UILabel *)freightPriceLb {
    if (!_freightPriceLb) {
        _freightPriceLb = [UILabel new];
        _freightPriceLb.text = @"￥666.00";
    }
    return _freightPriceLb;
}
- (UIView *)line3 {
    if (!_line3) {
        _line3 = [UIView new];
        _line3.backgroundColor = XJCOLOR_RGB(240, 240, 240);
    }
    return _line3;
}
//---------------------------------------------------


- (UIView *)div4 {
    if (!_div4) {
        _div4 = [UIView new];
    }
    return _div4;
}
- (UILabel *)liuYanLb {
    if (!_liuYanLb) {
        _liuYanLb = [UILabel new];
        _liuYanLb.text = @"留言";
    }
    return _liuYanLb;
}

- (UITextField *)testTF {
    if (!_testTF) {
        _testTF = [[UITextField alloc] init];
        _testTF.backgroundColor = [UIColor blueColor];
        _testTF.placeholder = @"请留言";
    }
    return _testTF;
}

//- (UITextField *)liuyanTextField {
//    if (!_liuyanTextField) {
//        _liuyanTextField  = [[UITextField alloc] init];
//        _liuyanTextField.enabled = YES;
//        _liuyanTextField.placeholder = @"请填写/输入对本次交易的留言实名";
//        _liuyanTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    }
//    return _liuyanTextField;
//}
- (UIView *)line4 {
    if (!_line4) {
        _line4 = [UIView new];
        _line4.backgroundColor = XJCOLOR_RGB(240, 240, 240);
    }
    return _line4;
}
//@property(nonatomic, strong) UIView *div4; // 买家留言 [就是好就是好就是好就是好]
//@property(nonatomic, strong) UILabel *liuYanLb;// 留言
//@property(nonatomic, strong) UITextField *liuyanTextField; // 请输入留言内容
//@property(nonatomic, strong) UIView *line4;
//---------------------------------------------------


#pragma mark -  三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    CGFloat kDivH = 50;
    CGFloat kLeftMargin = 15.0;

    [self div1LayoutWithDiwH:kDivH kLeftMargin:kLeftMargin];
    //----------------------------------------------------
    
    
    [self div2LayoutWithDivH:kDivH kLeftMargin:kLeftMargin];
    //----------------------------------------------------
    
    
    [self div5LayoutWithDivH:kDivH kLeftMargin:kLeftMargin];
    
    
    [self div3LayoutWithDivH:kDivH kLeftMargin:kLeftMargin];
    //----------------------------------------------------
    
    
    [self div4LayoutWithDivH:kDivH kLeftMargin:kLeftMargin];
    //----------------------------------------------------
}

- (void)div1LayoutWithDiwH:(CGFloat)kDivH kLeftMargin:(CGFloat)kLeftMargin {
        [self.div1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.bgView);
        make.height.mas_equalTo(kDivH);
    }];
    [self.totalLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div1.mas_left).offset(kLeftMargin);
        make.size.mas_equalTo(CGSizeMake(150, 30));
        make.centerY.equalTo((self.div1.mas_centerY));
    }];
    [self.totalPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.totalLb.mas_right);
        make.size.mas_equalTo(CGSizeMake(120, 30));
        make.centerY.equalTo(self.div1.mas_centerY);
    }];
    
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div1.mas_left).offset(kLeftMargin);
        make.height.mas_equalTo(1.0);
        make.right.equalTo(self.div1.mas_right).offset(-kLeftMargin);
        make.bottom.equalTo(self.div1.mas_bottom).offset(-1);
    }];
    
}



- (void)div2LayoutWithDivH:(CGFloat)kDivH kLeftMargin:(CGFloat)kLeftMargin {
    [self.div2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.bgView);
        make.top.equalTo(self.div1.mas_bottom);
        make.height.mas_equalTo(kDivH);
    }];
    [self.peiSongFangshiLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div2.mas_left).offset(kLeftMargin);
        make.centerY.equalTo(self.div2.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    
    // 左边参照 自提 按钮
    [self.kuaiDiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.ziTiButton.mas_left).offset(-kLeftMargin);
        make.size.mas_equalTo(CGSizeMake(80, 35));
        make.centerY.equalTo(self.div2.mas_centerY);
    }];
    [self.ziTiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.div2.mas_right).offset(-kLeftMargin);
        make.size.mas_equalTo(CGSizeMake(80, 35));
        make.centerY.equalTo(self.div2.mas_centerY);
    }];
    
    //线
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div2.mas_left).offset(kLeftMargin);
        make.height.mas_equalTo(1.0);
        make.right.equalTo(self.div2.mas_right).offset(-kLeftMargin);
        make.bottom.equalTo(self.div2.mas_bottom).offset(-1);
    }];
}

// 点击"自提"显示提货地点
- (void)div5LayoutWithDivH:(CGFloat)kDivH kLeftMargin:(CGFloat)kLeftMargin {
    // 点击自提显示提货地点
    [self.div5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.bgView);
        make.top.equalTo(self.div2.mas_bottom);
        make.height.mas_equalTo(kDivH);
    }];
    [self.tiHuoLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div5.mas_left).offset(kLeftMargin);
        make.centerY.equalTo(self.div5.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    
    // OK✅ 方式一 搞不定时打开此注释
    //    [self.tiHuoAddressLB mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(self.tiHuoLb.mas_right);
    //        make.top.bottom.equalTo(self.div5);
    //    }];
    
    // OK✅ 方式二
    [self.tiHuoAddressLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tiHuoLb.mas_right);
        make.right.equalTo(self.div5.mas_right).offset(-10);
        make.top.bottom.equalTo(self.div5);
    }];
    
    //线
    [self.line5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div5.mas_left).offset(kLeftMargin);
        make.height.mas_equalTo(1.0);
        make.right.equalTo(self.div5.mas_right).offset(-kLeftMargin);
        make.bottom.equalTo(self.div5.mas_bottom).offset(-1);
    }];
    
}



- (void)div3LayoutWithDivH:(CGFloat)kDivH kLeftMargin:(CGFloat)kLeftMargin {
    [self.div3 mas_makeConstraints:^(MASConstraintMaker *make) { // div3
        make.top.equalTo(self.div5.mas_bottom);
        make.left.right.equalTo(self.bgView);
        make.height.mas_equalTo(kDivH);
    }];
    
    [self.freightLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div3.mas_left).offset(kLeftMargin);
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.centerY.equalTo(self.div3);
    }];
    [self.freightPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.div3).offset(-kLeftMargin);
        make.size.mas_equalTo(CGSizeMake(120, 30));
        make.centerY.equalTo(self.div3.mas_centerY);
    }];
  
    
    //线
    [self.line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div3.mas_left).offset(kLeftMargin);
        make.height.mas_equalTo(1.0);
        make.right.equalTo(self.div3.mas_right).offset(-kLeftMargin);
        make.bottom.equalTo(self.div3.mas_bottom).offset(-1);
    }];
}

- (void)div4LayoutWithDivH:(CGFloat)kDivH kLeftMargin:(CGFloat)kLeftMargin {
    [self.div4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.bgView);
        make.top.equalTo(self.div3.mas_bottom);
        make.height.mas_equalTo(kDivH);
    }];
    
    [self.liuYanLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div4.mas_left).offset(kLeftMargin);
        make.centerY.equalTo(self.div4.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
//    [self.liuyanTextField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.liuYanLb.mas_right);
//        make.right.equalTo(self.div4.mas_right).offset(-kLeftMargin);
//        make.centerY.equalTo(self.div4.mas_centerY);
//    }];
    
    [self.testTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.liuYanLb.mas_right);
        make.right.equalTo(self.div4.mas_right).offset(-kLeftMargin);
        make.centerY.equalTo(self.div4.mas_centerY);
        make.height.mas_equalTo(40);
    }];
    
    //线
    [self.line4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div4.mas_left).offset(kLeftMargin);
        make.height.mas_equalTo(1.0);
        make.right.equalTo(self.div4.mas_right).offset(-kLeftMargin);
        make.bottom.equalTo(self.div4.mas_bottom).offset(-1);
    }];
}

#pragma mark -  点击事件
// 偶然发现一个bug:如果连续点击两下这个按钮,div5就永远显示不出来了。
- (void)kuaiDiButtonAction:(UIButton *)btn {
     NSLog(@"点快递");
    btn.selected = !btn.selected;
    
    // 解决上述bug,已经隐藏了
    if (self.div5.hidden) {
        return;
    }
    
    self.ziTiButton.selected = NO;
    self.div5.hidden = YES;
    
    // ✅ 这个不能用update更新约束 会有bug。？？？？ //不能这样用会有警告 
//    [self.div5 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(0);
//    }];
    self.div5.rb_height = 0;
}

- (void)ziTiButtonAction:(UIButton *)btn {
    NSLog(@"点自提");
    btn.selected = !btn.selected;
    self.kuaiDiButton.selected = NO;
    
    self.div5.hidden = NO;
    // 必须要更新约束 ✅ .不然无效
    [self.div5 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.div2.mas_bottom);
        make.left.right.equalTo(self.bgView);
        make.height.mas_equalTo(50);
    }];
}


#pragma mark - 私有方法
// 颜色生成图片
- (UIImage *)imageWithColor:(UIColor *)color{
    //rect的width和height不能为0
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


#pragma mark - configure
- (void)configureSureOrderSectionFooterViewCnt:(NSString *)cnt totalPrice:(NSString *)totalPrice tihuoAddress:(NSString *)tihuoAddress freight:(NSString *)freight isShowKuaiDi:(BOOL)showKuaiDi{
    
    // 共计n件商品
    self.totalLb.text = [NSString stringWithFormat:@"共计%@件商品,小计",cnt];
    
    
    // 小计
    self.totalPriceLb.text = [NSString stringWithFormat:@"%.1f",[totalPrice floatValue]];
    
    // 提货地点
    if ([NSString isEmptyString:tihuoAddress]) {
        self.tiHuoAddressLB.text = @"";
    }else {
        self.tiHuoAddressLB.text = tihuoAddress;
    }
    
    // 运费
    self.freightPriceLb.text = [NSString stringWithFormat:@"￥%.1f",[freight floatValue]];
    
    
    //默认是快递 自提界面不显示
    if (showKuaiDi) {
        [self kuaiDiButtonAction:self.kuaiDiButton];
    }
}


@end
